using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace GameFrameworkLibraly.BaseObjects
{
    /// <summary>
    /// List of game objects that supports removing from itself at any moment
    /// </summary>
    /// <typeparam name="T">Type that inherits GameObjectBase</typeparam>
    public class GameObjectsList<T> where T: GameObjectBase
    {
        private T[] _objectArray;

        /// <summary>
        /// List of game objects
        /// </summary>
        public List<T> GameObjects;

        /// <summary>
        /// Updates all the elements from this list
        /// </summary>
        /// <param name="gameTime"></param>
        public virtual void UpdateAll(GameTime gameTime)
        {
            int i;
            int objectCount = GameObjects.Count;

            if (_objectArray == null)
            {
                _objectArray = new T[(int)MathHelper.Max(20, GameObjects.Count * 1.2f)];
            }
            else if (GameObjects.Count > _objectArray.Length)
            {
                _objectArray = new T[(int)(GameObjects.Count * 1.2f)];
            }

            for (i = 0; i < _objectArray.Length; i++)
            {
                if (i < GameObjects.Count)
                {
                    _objectArray[i] = GameObjects[i];
                }
                else
                {
                    _objectArray[i] = null;
                }
            }

            for (i = 0; i < objectCount; i++)
            {
                _objectArray[i].Update(gameTime);
            }
        }
    }
}
